package com.core.zyyz.util;

import com.core.zyyz.amc.modules.domain.BaseDictionary;
import com.core.zyyz.pcmc.service.impl.ValueSetDictImpl;
import com.google.common.collect.Sets;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;

import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

public class ValueSetUtils {
    private ValueSetUtils() {
    }

    public static List<BaseDictionary> get(String valueSetCode) {
        return new ValueSetDictImpl(valueSetCode).get();
    }

    public static BaseDictionary get(String valueSetCode, String dictCode) {
        List<BaseDictionary> list = get(valueSetCode);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        } else {
            for(BaseDictionary dict : list){
                if (dict.getValue().equals(dictCode)){
                    return dict;
                }
            }
            return null;
        }
    }

    public static BaseDictionary getByDictName(String valueSetCode, String dictName) {
        List<BaseDictionary> list = get(valueSetCode);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        } else {
            for(BaseDictionary dict : list){
                if (dict.getLabel().equals(dictName)){
                    return dict;
                }
            }
            return null;
        }
    }

    public static List<BaseDictionary> get(String valueSetCode, List<String> includes, List<String> excludes) {
        List<BaseDictionary> dictList = get(valueSetCode);
        return get(dictList, includes, excludes);
    }

    public static List<BaseDictionary> get(List<BaseDictionary> dictList, List<String> includes, List<String> excludes) {
        if (CollectionUtils.isEmpty(dictList)) {
            return Lists.newArrayList();
        } else {
            if (CollectionUtils.isNotEmpty(includes)) {
                HashSet excludeSet = Sets.newHashSet(includes);
                dictList = (List)dictList.stream().filter((dict) -> {
                    return excludeSet.contains(dict.getValue());
                }).collect(Collectors.toList());
            }

            if (CollectionUtils.isNotEmpty(excludes)) {
                HashSet excludeSet = Sets.newHashSet(excludes);
                dictList = (List)dictList.stream().filter((dict) -> {
                    return !excludeSet.contains(dict.getValue());
                }).collect(Collectors.toList());
            }

            return dictList;
        }
    }
}
